<template>
  <div id="approveconfirmcomp">
    <!-- Form-开始 -->
    <vxe-modal :title="`授权验证`" width="500px" v-model="dialogFormVisible" draggable>
      <vxe-form ref="pagepanelnameForm" :data="form" :title-width="'100px'" className="margin-bottom-sm">
        <vxe-form-item field="name" title="用户名称" span="24">
          <VxePulldownSelection ref="employeeSelectRef" :url="'/api/user/getUserInfoList'" @confirmClick="SelectRowClick" :tableColumn="[{ title: '编号', field: 'username' }, { title: '名称', field: 'realname' }]" :aimKeys="[{ formKey: 'code', rowKey: 'username' },{ formKey: 'id', rowKey: 'id' }, { formKey: 'name', rowKey: 'realname' }]" :displayTexts="['realname']" :displayTextObj="form.code" />
        </vxe-form-item>
        <vxe-form-item field="name" title="授权码" span="24">
          <vxe-input ref="pswInput" v-model="form.password" autocomplete="new-password" name="password" type="password" auto-complete="new-password" clearable />
        </vxe-form-item>
      </vxe-form>
      <div class="flex justify-end">
        <el-button type="primary" v-loading="showLoading" @click="cacheEmployeeInfo()">确定</el-button>
        <el-button @click="closeDialog()"> 取消</el-button>
      </div>
    </vxe-modal>
    <!-- Form-结束 -->
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, getCurrentInstance, watch, onMounted } from 'vue';
import { pageSizes, DateToString, formatDateTime, IsEmpty } from '@/utils/common';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { VXETable } from 'vxe-table';
import XEUtils from 'xe-utils';
import { AppStore } from '@/stores/store';//'../../../stores/store';
import axios from 'axios';
import SelectDataList from '@/components/SelectDataList.vue';
import VxePulldownSelection from '@/components/VxePulldownSelection.vue';
import { getOptionValueByCodeRptGroupOption } from '@/utils/SysOptionsHelper';

// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
let store = AppStore();
let showLoading = ref(false);

let dialogFormVisible = ref(false);

let props = defineProps({
  isLocall: String, //是否存储到本地
  IsShow: Boolean,
  origin: Number //来源，弹框在哪个地方打开的。1:快捷菜单,2:批次审核
}) as any;
const pswInput = ref(null);//密码框el对象
const employeeSelectRef = ref(null);//用户选择组件el对象
let curAction = ref('1');/**当前动作标记【1:初审、审核按钮触发；2:编辑左侧标本信息触发；3:全局组件快捷菜单触发】*/
defineExpose({
  initPage
})
onMounted(() => {
  //ToDo
});
/**接收父组件传过来的弹框展开状态
 * @param triggerSource 事件触发源【1：取消审核验证取消审核者、2：取消审核验证 检验者、3:微生物发布二级报告】 
 */
function initPage(triggerSource: string, user: any) {
  form.id = '';
  form.code = '';
  form.name = '';
  curAction.value = '';

  dialogFormVisible.value = true;
  /**判断事件来源 */
  if (!IsEmpty(triggerSource)) {
    curAction.value = triggerSource;
  }
  /**左侧编辑病人信息调用时 会传选择的人进来 */
  if (user && !IsEmpty(user.username)) {
    proxy.$nextTick(() => {
      form.code = user.username;
      form.id = user.id;
      form.name = user.realname;
      if (employeeSelectRef.value) {
        employeeSelectRef.value?.resetDisplayText(form.code, () => {
          pswInputFocus();
        });
      }
      pswInputFocus();
    });
  }
}

//   //#region  变量
let form = reactive({ seachValue: '', id: '', name: '', code: '', password: '' });
let loading = ref(false);

//   //#region  Methods
/**密码框获取焦点 */
function pswInputFocus() {
  if (!pswInput.value) {
    setTimeout(() => {
      pswInputFocus();
    }, 500);
    return;
  }
  proxy.$nextTick(() => {
    pswInput.value?.focus();
  });
}
// 点击组件表格行
function SelectRowClick(e) {
  let a = {} as any;
  e.aimKeys.forEach(item => {
    a[item.formKey] = e.row[item.rowKey];
  });
  Object.assign(form, a);
}

// 取消操作
function closeDialog() {
  dialogFormVisible.value = false;
  form = { seachValue: '', id: '', name: '', code: '', password: '' };
}

async function cacheEmployeeInfo() {
  if (IsEmpty(form.name) || IsEmpty(form.code)) {
    VxeMessage('请选择医师！', VxeNotifyType.error);
    return;
  }
  if (IsEmpty(form.password)) {
    VxeMessage('请输入授权码!', VxeNotifyType.error);
    return;
  }
  const data = { params: { username: form.code, empowerNum: form.password } };
  loading.value = true;
  const res = await new Promise((resolve, reject) => {
    proxy.$http
      .get('/api/user/checkEmpowerNum', data)
      .then((res: any) => {
        if (res && res.status && res.status.code && res.status.code === 1 && res.data) {
          resolve(true);
        } else {
          resolve(false);
        }
      })
      .catch((err: any) => {
        resolve(false);
      });
  });

  /** 第一步, 如果是取消审核验证，直接返回*/
  if (res) {
    proxy.$emit('unChkConfirm', { code: form.code, name: form.name });
    dialogFormVisible.value = false;
  }
  /**第三步, */
  /**第四步, */
  /**最后一步,如果授权失败,清楚记忆的缓存数据 */
  if (!res) {
    VxeMessage('授权验证失败', VxeNotifyType.error);
  }
  loading.value = false;
}
</script>
<style lang="scss" scope>
#pagepanelname {
  .tool {
    margin: 10px 15px;

    .el-row {
      .tool-left,
      .tool-right {
        background-color: bg-white;
      }

      .el-form-item {
        margin-top: 10px;
        margin-bottom: 10px;
      }
    }
  }

  .el-divider--vertical {
    height: 20px;
  }
}

.readio-item {
  height: 28px;
  line-height: 28px;
  vertical-align: middle;
}

.readio-item > label {
  margin-top: calc((28px - 16px) / 2);
}

.readio-item > div > label {
  margin-top: calc((28px - 16px) / 2);
}
</style>
